import { defineStore } from 'pinia';
import tmuiconfigdefault from '@/tool/lib/tmuiconfigDefault';
import themeColor from '@/tool/theme/theme';
import * as u from '@/tool/function/util';
import type { systemConfigState, wxshareConfig, colorThemeType } from '@/tool/lib/interface';
let pdefault_cookies_color = u.getCookie('setThemeColor') || '';
let pdefault_cookies_black = u.getCookie('setThemeBlack');
let pdefault_cookies_local = u.getCookie('setThemeLocal') || 'zh-Hans';
let pdefault_cookies_colorArrayList = u.getCookie('colorArrayList');
let dark = typeof pdefault_cookies_black === 'boolean' ? pdefault_cookies_black : false;
let themeObj = new themeColor.themeColors();
if (pdefault_cookies_colorArrayList) {
  const result2 = pdefault_cookies_colorArrayList.filter((item: colorThemeType) => themeObj.colors.every((subItem) => subItem.name !== item.name));
  themeObj = new themeColor.themeColors([...themeObj.colors, ...result2]);
}
const colorArray = themeObj.colors;
const os = uni.getSystemInfoSync()?.osName ?? '';
u.setCookie('colorArrayList', colorArray);

const systemConfigStore = defineStore('systemConfigStore', {
  state: (): systemConfigState => {
    return {
      color: pdefault_cookies_color,
      dark: dark,
      pages: '',
      pagesIndex: '',
      os: os,
      //这里是微信小程序和微信H5的配置资料。
      wxshareConfig_miniMp: {
        title: '', // 分享标题
        desc: '', // 描述
        imageUrl: '', // 分享图片
        path: '', // 分享路径
        copyLink: '', // 复制链接
        query: {}, // 分享参数
      },
      //当前存储存的主题对象。
      colorList: colorArray,
      //当前的语言
      local: pdefault_cookies_local,
      uiStyle: 'normal', //normal:标准模式  elder:适老模式 default 默认
      showNav: false, //跳转第三方页面是否显示导航栏
      areaCode: '330783',
      areaName: '东阳市',
      browerInfo: {}, //浏览器类型
      defaultAESKey: 'eGlhb3l5TG92ZXpoQW5kSA==',//默认AES秘钥
      defaultSM4Key: '79797869616f68687869616f63637a68',//默认秘钥
      defaultSM2PublicKey: '04e0ed59fca10e44b0256b836830ecd236a12cc3b4f97ddc57a2be207af17b820f63f6912a83eccebde0fd88057fbbea1e1113b2cf2d7476381ed327679c7b084e',//默认公钥
			defaultSM2PrivateKey: '26b39e08bee21603e4906c76067ed87d4ec93481593fc19c04989181841681f1',//默认私钥
      showNavBar: true, //是否显示顶部标题导航栏
      zlbFlag: false, //是否浙里办
      autoDark: false,
      themeConfig: tmuiconfigdefault,
      appCryptoFlag:false,
      allApiCrytpo:false,//是接口否全不加密
      orientation:'',
    };
  },
  getters: {
    getAutoDark: (state) => {
      let pdefault_cookies_autoDark = u.getCookie('setAutoDark') ?? undefined;
      let autoDark = typeof pdefault_cookies_autoDark === 'boolean' ? pdefault_cookies_autoDark : state.autoDark;
      return autoDark;
    },
  },
  actions: {
    setPageNow(url: string) {
      this.pages = url;
    },
    setPageNowIndex(index: string) {
      this.pagesIndex = index;
    },
    setThemeDark(dark: boolean) {
      dark = typeof dark !== 'boolean' ? false : dark;
      u.setCookie('setThemeBlack', dark);
      this.dark = dark;
    },
    setZlbFlag(flag: boolean) {
      flag = typeof flag !== 'boolean' ? false : flag;
      this.zlbFlag = flag;
    },
    setShowNavBar(flag: boolean) {
      flag = typeof flag !== 'boolean' ? true : flag;
      this.showNavBar = flag;
    },
    setAutoDark(autoDark = false) {
      u.setCookie('setAutoDark', autoDark);
      this.autoDark = autoDark;
      if (autoDark) {
        let nowstrdark = '';
        // #ifdef H5
        if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
          nowstrdark = 'dark';
        } else {
          nowstrdark = 'light';
        }
        // #endif

        // #ifndef H5
        nowstrdark = uni.getSystemInfoSync()?.osTheme ?? '';
        // #endif
        this.setThemeDark(nowstrdark == 'dark' ? true : false);
      }
    },
    setWxShare(cfg: wxshareConfig) {
      let pcf = cfg || {};
      if (typeof pcf !== 'object' || Array.isArray(cfg)) pcf = {};

      this.wxshareConfig_miniMp = {
        ...this.wxshareConfig_miniMp,
        ...pcf,
      };
    },
    setTheme(color: string) {
      let defaultColorName = color;
      if (!defaultColorName || defaultColorName == '' || themeColor.isCssColor(defaultColorName)) {
        defaultColorName = '';
      }
      u.setCookie('setThemeColor', defaultColorName);
      this.color = defaultColorName;
    },
    //添加一个主题
    setAddTheme(colorName: string, color: string, isSet = true) {
      this.colorList = themeObj.add(colorName, color);
      u.setCookie('colorArrayList', this.colorList);
      if (isSet) {
        this.setTheme(colorName);
      }
    },
    setLocal(language: string) {
      language = language || 'zh-Hans';
      u.setCookie('setThemeLocal', language);
      this.local = language;
    },
    setBrowerInfo(obj: object) {
      this.browerInfo = obj;
    },
    setUiStyle(str: string) {
      this.uiStyle = str;
    },
    setShowNav(obj: boolean) {
      this.showNav = obj;
    },
    setAreaCode(str: string) {
      this.areaCode = str;
    },
    setAreaName(str: string) {
      this.areaName = str;
    },
    setDefaultSM2PublicKey(str: string) {
      this.defaultSM2PublicKey = str;
    },
    setDefaultSM2PrivateKey(str: string) {
      this.defaultSM2PrivateKey = str;
    },
    setAppCryptoFlag(str: boolean) {
      this.appCryptoFlag = str;
    },
  },
});
export default systemConfigStore;
